package org.msss.sprites;

/*
 * This is probably one of the most important classes, so it could use a lot of fixing
 * and making the code cleaner.
 */

import org.msss.gamebasic.*;
import org.msss.resource.ImageDB;
import static org.msss.gamebasic.Game.*;
import org.lwjgl.input.Keyboard;

/**
 * The player...
 * 
 * @author alanxoc3
 * 
 */
public class Player extends Sprite {

	private final int WALK_ACCEL = 3;
	private final int RUN_ACCEL = 6;
	private final int RUN_WAIT = Frame.FRAMERATE/10;
	private final double FRICTION = .25;
	private double xSpeed, ySpeed;
	private int leftTimer, rightTimer, upTimer, downTimer;
	private boolean leftTimerOn, rightTimerOn, upTimerOn, downTimerOn;
	private boolean leftRun, rightRun, upRun, downRun;

	@Override
	public void create() {
		setImage(ImageDB.head_down);
		setZ(-900);
		
		xSpeed = 0;
		ySpeed = 0;
		leftTimer = 0;
		leftTimerOn = false;
		leftRun = false;
	}

	//NOTE: Diagonal movement is not accounted for here.
	//		to do that, we would need to use trig (pythagorean theorum)
	//		I (alan) may get to it later if someone else doesn't
	@Override
	public void down(int keyDown) {
		
		int rightAccel = 0;
		int downAccel = 0;
		int upAccel = 0;
		int leftAccel = 0;
		
		if (leftRun) {
			leftAccel = RUN_ACCEL;
		} else {
			leftAccel = WALK_ACCEL;
		}
		
		if (rightRun) {
			rightAccel = RUN_ACCEL;
		} else {
			rightAccel = WALK_ACCEL;
		}
		
		if (upRun) {
			upAccel = RUN_ACCEL;
		} else {
			upAccel = WALK_ACCEL;
		}
		
		if (downRun) {
			downAccel = RUN_ACCEL;
		} else {
			downAccel = WALK_ACCEL;
		}
		
		switch (keyDown) {
		case Keyboard.KEY_RIGHT:
			xSpeed += rightAccel;
			break;
		case Keyboard.KEY_LEFT:
			xSpeed -= leftAccel;
			break;
		case Keyboard.KEY_UP:
			ySpeed -= upAccel;
			break;
		case Keyboard.KEY_DOWN:
			ySpeed += downAccel;
			break;
		}
	}

	@Override
	public void released(int keyReleased) {
		//LEFT
		if (keyReleased == Keyboard.KEY_LEFT) {
			leftTimerOn = true;
			leftRun = false;
		}
		//RIGHT
		else if (keyReleased == Keyboard.KEY_RIGHT) {
			rightTimerOn = true;
			rightRun = false;
		}
		//DOWN
		else if (keyReleased == Keyboard.KEY_DOWN) {
			downTimerOn = true;
			downRun = false;
		}
		//UP
		else if (keyReleased == Keyboard.KEY_UP) {
			upTimerOn = true;
			upRun = false;
		}
	}
	
	@Override
	public void pressed(int keyPressed) {
		//LEFT
		if (keyPressed == Keyboard.KEY_LEFT) {
			if (leftTimerOn) {
				leftRun = true;
				leftTimerOn = false;
			} else {
				leftRun = false;
			}
		}
		//RIGHT
		else if (keyPressed == Keyboard.KEY_RIGHT) {
			if (rightTimerOn) {
				rightRun = true;
				rightTimerOn = false;
			} else {
				rightRun = false;
			}
		}
		//DOWN
		else if (keyPressed == Keyboard.KEY_DOWN) {
			if (downTimerOn) {
				downRun = true;
				downTimerOn = false;
			} else {
				downRun = false;
			}
		}
		//UP
		else if (keyPressed == Keyboard.KEY_UP) {
			if (upTimerOn) {
				upRun = true;
				upTimerOn = false;
			} else {
				upRun = false;
			}
		}
	}
	
	@Override
	public void move() {
		addFriction();
		updateRunTimer();

		setX(getX() + (int)xSpeed);
		setY(getY() + (int)ySpeed);

		view.setX(getX() - view.getW() / 2);
		view.setY(getY() - view.getH() / 2);
	}

	@Override
	public void collision() {
		
	}
	
	/**
	 * Adds friction to the player.
	 */
	public void addFriction() {
		xSpeed = xSpeed - xSpeed * FRICTION;
		ySpeed = ySpeed - ySpeed * FRICTION;
	}
	
	/**
	 * Updates the players timers used for running.
	 */
	public void updateRunTimer() {
		//LEFT
		if (leftTimer > RUN_WAIT) {
			leftTimerOn = false;
		}
		
		if (leftTimerOn) {
			leftTimer++;
		} else {
			leftTimer = 0;
		}
		//DOWN
		if (downTimer > RUN_WAIT) {
			downTimerOn = false;
		}
		
		if (downTimerOn) {
			downTimer++;
		} else {
			downTimer = 0;
		}
		//UP
		if (upTimer > RUN_WAIT) {
			upTimerOn = false;
		}
		
		if (upTimerOn) {
			upTimer++;
		} else {
			upTimer = 0;
		}
		//RIGHT
		if (rightTimer > RUN_WAIT) {
			rightTimerOn = false;
		}
		
		if (rightTimerOn) {
			rightTimer++;
		} else {
			rightTimer = 0;
		}
	}
}
